using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TaugeLibrary.Classes;
using System.Collections;
using TaugeFramework.Classes;

namespace TaugeFramework.Forms
{
    partial class frmLookup : Form
    {
        private DataTable m_dt;
        private object[] m_returnvalues;

        private string[] m_returncolumns;
        private string m_type;
        private BindingSource m_bs = new BindingSource();

        private Database m_db;

        public object Value
        {
            get { if (m_returnvalues == null) return null; else return m_returnvalues[0]; }
        }

        public object[] Values
        {
            get { return m_returnvalues; }
        }
        
        public frmLookup(string p_sql, string[] p_returncolumns, string p_default_column_search, string p_title, Database p_db)
        {
            InitializeComponent();

            m_db = p_db;

            this.Text = p_title;
            cboOperator.Items.Clear();
            cboOperator.Items.Add("=");
            cboOperator.Items.Add("<=");
            cboOperator.Items.Add("<");
            cboOperator.Items.Add(">=");
            cboOperator.Items.Add(">");
            cboOperator.Items.Add("<>");
            cboOperator.SelectedIndex = 0;

            /*PENDING
            m_dt = m_db.executeDataTable(p_sql.Replace("@CSLIST", Program.ActiveUser.getCSList())
                                                .Replace("@CSID", Constants.sqlStr(Program.ActiveCompany.CompanyStructureID))
                                                );
            */
            m_dt = m_db.executeDataTable(p_sql);
            
            m_returncolumns = p_returncolumns;
            m_bs.DataSource = m_dt;
            dgData.DataSource = m_bs;
            dgData.Sort(dgData.Columns[m_returncolumns[0]], System.ComponentModel.ListSortDirection.Ascending);
            for (int i = 0; i < dgData.Columns.Count; i++)
                cboField.Items.Add(dgData.Columns[i].Name);

            if (p_default_column_search != "")
                cboField.Text = p_default_column_search;
            else
                cboField.Text = m_returncolumns[0];
            TaugeLibrary.Classes.LibConstants.TidyGrid(dgData);
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            m_returnvalues = null;
            this.Close();
        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtValue.Text == "")
                    m_bs.Filter = "";
                else
                    if (m_type == "System.String")
                    {
                        if (cboOperator.Text == "=")
                            m_bs.Filter = cboField.Text + " LIKE '" + txtValue.Text.Replace("'", "''") + "%'";
                        else
                            m_bs.Filter = cboField.Text + cboOperator.Text + LibConstants.toSql(txtValue.Text);
                    }
                    else if (m_type == "System.DateTime")
                    {
                        TaugeDateTime xdate = new TaugeDateTime();
                        xdate.DateText = txtValue.Text;
                        if (!xdate.IsNull)
                            m_bs.Filter = cboField.Text + cboOperator.Text + xdate.SqlDate;
                    }
                    else if (m_type == "System.Int32" || m_type == "System.Integer" || m_type == "System.Decimal")
                        m_bs.Filter = cboField.Text + cboOperator.Text + Convert.ToDouble(txtValue.Text).ToString();
                    else if (m_type == "System.Boolean")
                    {
                        if ("|B|T|Y|1|".IndexOf(txtValue.Text.Substring(0, 1).ToUpper()) > 0)
                            m_bs.Filter = cboField.Text + cboOperator.Text + "1";
                        else
                            m_bs.Filter = cboField.Text + cboOperator.Text + "0";
                    }

                    else
                        m_bs.Filter = cboField.Text + cboOperator.Text + txtValue.Text;
            }
            catch
            {
            }

        }

        private void cboField_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewColumn x = dgData.Columns[cboField.Text];
                m_type = dgData.Columns[cboField.Text].ValueType.ToString();
                txtValue.Text = "";
                dgData.Sort(x, System.ComponentModel.ListSortDirection.Ascending);

                txtValue_TextChanged(null, null);
                txtValue.Focus();
            }
            catch { }


        }

        private void dgData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (m_dt.Rows.Count <= 0)
                    m_returnvalues = null;
                ArrayList ar = new ArrayList();
                foreach (string s in m_returncolumns)
                    ar.Add(dgData[s, e.RowIndex].Value);
                m_returnvalues = ar.ToArray();
            }
            catch
            {
                m_returnvalues = null;
            }
            this.Close();

        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_dt.Rows.Count <= 0)
                    m_returnvalues = null;
                ArrayList ar = new ArrayList();
                foreach (string s in m_returncolumns)
                    ar.Add(dgData[s, dgData.SelectedRows[0].Index].Value);
                m_returnvalues = ar.ToArray();
            }
            catch
            {
                m_returnvalues = null;
            }
            this.Close();

        }

        private void frmLookup_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cmdOK_Click(null, null);
            txtValue.Focus();
        }

        private void dgData_KeyDown(object sender, KeyEventArgs e)
        {
            frmLookup_KeyDown(null, e);
        }

        private void txtValue_KeyDown(object sender, KeyEventArgs e)
        {
            frmLookup_KeyDown(null, e);
            if (e.KeyCode == Keys.PageDown || e.KeyCode == Keys.PageUp || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
            {
                dgData.Focus();
                switch (e.KeyCode)
                {
                    case Keys.PageDown:
                        SendKeys.Send("{PGDN}");
                        break;
                    case Keys.PageUp:
                        SendKeys.Send("{PGUP}");
                        break;
                    case Keys.Down:
                        SendKeys.Send("{DOWN}");
                        break;
                    case Keys.Up:
                        SendKeys.Send("{UP}");
                        break;
                }
            }

        }



        private void dgData_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            cboField.Text = dgData.Columns[e.ColumnIndex].Name;
            txtValue.Focus();
        }

        private void cboOperator_TextChanged(object sender, EventArgs e)
        {
            txtValue_TextChanged(null, null);
            txtValue.Focus();
        }

        private void frmLookup_Activated(object sender, EventArgs e)
        {
            txtValue.Focus();
        }

        private void dgData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgData_Click(object sender, EventArgs e)
        {
            txtValue.Focus();
        }

        private void txtValue_TextChanged_1(object sender, EventArgs e)
        {

        }

 
    }
}